// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.characters.action;

import java.util.ArrayList;
import java.util.List;
import org.w3c.dom.Element;
import zombie.characters.IsoPlayer;
import zombie.characters.IsoZombie;
import zombie.core.Core;
import zombie.debug.DebugLog;
import zombie.debug.DebugOptions;
import zombie.debug.DebugType;
import zombie.network.GameClient;
import zombie.util.Lambda;
import zombie.util.PZXmlUtil;
import zombie.util.StringUtils;

public final class ActionTransition implements Cloneable {
    String transitionTo;
    boolean asSubstate;
    boolean transitionOut;
    boolean forceParent;
    final List<IActionCondition> conditions = new ArrayList();

    public static boolean parse(Element root, String srcInfo, List<ActionTransition> out_transitions) {
        if (root.getNodeName().equals("transitions")) {
            parseTransitions(root, srcInfo, out_transitions);
            return true;
        } else if (root.getNodeName().equals("transition")) {
            parseTransition(root, out_transitions);
            return true;
        } else {
            return false;
        }
    }

    public static void parseTransition(Element root, List<ActionTransition> out_transitions) {
        out_transitions.clear();
        ActionTransition actionTransition = new ActionTransition();
        if (actionTransition.load(root)) {
            out_transitions.add(actionTransition);
        }
    }

    public static void parseTransitions(Element root, String srcInfo, List<ActionTransition> out_transitions) {
        out_transitions.clear();
        Lambda.forEachFrom(PZXmlUtil::forEachElement, root, srcInfo, out_transitions, (element, string, list) -> {
            if (!element.getNodeName().equals("transition")) {
                DebugLog.ActionSystem.warn("Warning: Unrecognised element '" + element.getNodeName() + "' in " + string);
            } else {
                ActionTransition actionTransition = new ActionTransition();
                if (actionTransition.load(element)) {
                    list.add(actionTransition);
                }
            }
        });
    }

    private boolean load(Element element) {
        try {
            PZXmlUtil.forEachElement(element, elementx -> {
                try {
                    String string = elementx.getNodeName();
                    if ("transitionTo".equalsIgnoreCase(string)) {
                        this.transitionTo = elementx.getTextContent();
                    } else if ("transitionOut".equalsIgnoreCase(string)) {
                        this.transitionOut = StringUtils.tryParseBoolean(elementx.getTextContent());
                    } else if ("forceParent".equalsIgnoreCase(string)) {
                        this.forceParent = StringUtils.tryParseBoolean(elementx.getTextContent());
                    } else if ("asSubstate".equalsIgnoreCase(string)) {
                        this.asSubstate = StringUtils.tryParseBoolean(elementx.getTextContent());
                    } else if ("conditions".equalsIgnoreCase(string)) {
                        PZXmlUtil.forEachElement(elementx, elementxx -> {
                            IActionCondition iActionCondition = IActionCondition.createInstance(elementxx);
                            if (iActionCondition != null) {
                                this.conditions.add(iActionCondition);
                            }
                        });
                    }
                } catch (Exception exceptionx) {
                    DebugLog.ActionSystem.error("Error while parsing xml element: " + elementx.getNodeName());
                    DebugLog.ActionSystem.error(exceptionx);
                }
            });
            return true;
        } catch (Exception exception) {
            DebugLog.ActionSystem.error("Error while loading an ActionTransition element");
            DebugLog.ActionSystem.error(exception);
            return false;
        }
    }

    public String getTransitionTo() {
        return this.transitionTo;
    }

    public boolean passes(ActionContext context, int layerIdx) {
        for (int _int = 0; _int < this.conditions.size(); _int++) {
            IActionCondition iActionCondition0 = (IActionCondition)this.conditions.get(_int);
            if (!iActionCondition0.passes(context, layerIdx)) {
                return false;
            }
        }

        if (Core.bDebug
            && GameClient.bClient
            && (
                DebugOptions.instance.MultiplayerShowPlayerStatus.getValue()
                        && context.getOwner() instanceof IsoPlayer
                        && !((IsoPlayer)context.getOwner()).isGodMod()
                    || DebugOptions.instance.MultiplayerShowZombieStatus.getValue() && context.getOwner() instanceof IsoZombie
            )) {
            StringBuilder stringBuilder = new StringBuilder("Character ")
                .append(context.getOwner().getClass().getSimpleName())
                .append(" ")
                .append("id=")
                .append(context.getOwner().getOnlineID())
                .append(" transition to \"")
                .append(this.transitionTo)
                .append("\":");

            for (IActionCondition iActionCondition1 : this.conditions) {
                stringBuilder.append(" [").append(iActionCondition1.getDescription()).append("]");
            }

            DebugLog.log(DebugType.ActionSystem, stringBuilder.toString());
        }

        return true;
    }

    public ActionTransition clone() {
        ActionTransition actionTransition0 = new ActionTransition();
        actionTransition0.transitionTo = this.transitionTo;
        actionTransition0.asSubstate = this.asSubstate;
        actionTransition0.transitionOut = this.transitionOut;
        actionTransition0.forceParent = this.forceParent;

        for (IActionCondition iActionCondition : this.conditions) {
            actionTransition0.conditions.add(iActionCondition.clone());
        }

        return actionTransition0;
    }
}
